const { dest, src,series,parallel,watch } = require('gulp')
const loadPlugins = require('gulp-load-plugins')

const browserSync = require('browser-sync')

const plugin = new loadPlugins()
const bs = browserSync.create()


const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}


const clean = () => {
  return del(['dist', 'temp'])
}

const style = () => {
  
  return src('src/assets/styles/*.scss', { base: 'src' }).pipe(plugin.sass())
    .pipe(dest('dist'))
}

const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' }).pipe(plugin.babel({
    presets: ['@babel/preset-env']
  }))
    .pipe(dest('dist'))
}

const page = () => {
  return src("src/*.html", { base: 'src' }).pipe(plugin.swig({data})).pipe(dest('dist'))
}

const image = () => {
  return src("src/assets/images/**", { base: 'src' }).pipe(plugin.imagemin()).pipe(dest('dist'))
}

const fonts = () => {
  return src("src/assets/fonts/**", { base: 'src' }).pipe(plugin.imagemin()).pipe(dest('dist'))
}

const extra = () => {
  return src("public/**", { base: 'public' }).pipe(dest('dist'))
}

const serve = () => {
  watch("src/assets/styles/*.scss",style)
  watch("src/assets/scripts/*.js",script)
  watch("src/*.html",page)
  watch([
    'src/assets/images/**',
    'src/assets/scripts/*.js',
    'src/*.html'
  ],bs.reload)

  bs.init({
    server: {
      port:8080,
      baseDir:["dist","public","src"],
      files:'dist/**',
      routes: {
        '/node_modules': "node_modules"
      }
    }
  })
}
const compile = parallel(page, script)

const build = parallel(compile,fonts,image,extra)

const develop = series(compile,serve)

module.exports = {
  clean,
  compile,build,develop
}
